﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Text;

namespace Thv.admin.Pages
{
    public partial class kichhoat : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           // tempath = WebUtils.SystemUrl();
            if (!IsPostBack)
                Bindata();
        }

        void Bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.UserDBs
                       where k.flag == 1 && k.kichhoat == false
                       select k;
            grvTV.DataSource = list;
            grvTV.DataBind();
        }

        protected void grvTV_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            Bindata();
        }

        protected void btnKichhoat_Click(object sender, EventArgs e)
        {
            int id = (int)grvTV.GetRowValues(grvTV.FocusedRowIndex, "ID");
            int ket = kiemtra(id);
            if (ket == 1)
            {
                DataClasses1DataContext dx = new DataClasses1DataContext();
                UserDB u = new UserDB();
                u = dx.UserDBs.Single(p => p.ID == id);
                u.kichhoat = true;
                dx.SubmitChanges();
                Bindata();
            }
            else
                ShowPopUpMsg("Thông báo! Tài khoản chỉ định chưa kích hoạt nên bạn không có quyền kích hoạt tài khoản này");
        }

        private void ShowPopUpMsg(string msg)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("alert('");
            sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
            sb.Append("');");
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
        }
        int kiemtra(int id)
        {
            int ketqua = 0;
            int idcha=0;
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.UserDBs
                       where k.ID == id
                       select k;
            foreach(UserDB u in list)
                idcha = (int)u.parentID;

            var list2 = from k in dx.UserDBs
                        where k.ID == idcha
                        select k;
            foreach (UserDB u in list2)
                if (u.kichhoat == true) ketqua = 1;

            return ketqua;
        }
        void VisibleBook()
        {
            GridViewDataColumn column1 = this.grvTV.Columns["KH"] as GridViewDataColumn;
            for (int i = 0; i < grvTV.VisibleRowCount; i++)
            {
                HiddenField hdfId = (HiddenField)this.grvTV.FindRowCellTemplateControl(i, column1, "hdfIdcha");
                ImageButton btn = (ImageButton)this.grvTV.FindRowCellTemplateControl(i, column1, "btnKichhoat");

                
            }
        }
    }
}